<?php

namespace App\Http\Controllers\CRM;

use App\Enums\UserType;
use App\Helper\ApiResponseHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\CRM\UserRequest;
use App\Http\Resources\CRM\UserResource;
use App\Models\User;
use App\Models\UserRole;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    public function getCurrentInfo()
    {
        $user = Auth::user();
        $user->role;
        $user->store;

        return new UserResource($user);
    }

    public function create(UserRequest $request)
    {
        $existUser = User::withTelephone($request->telephone)->first();
        if (!empty($existUser))
            return ApiResponseHelper::json([], 400, '手机号已存在');

        $password = $request->password;
        if (empty($password))
            return ApiResponseHelper::json([], 400, '密码不能为空');

        $userRole = UserRole::find($request->role_id);
        if (empty($userRole))
            return ApiResponseHelper::json([], 400, '角色不存在');

        if ($userRole->type == UserType::Assistant->value && empty($request->assistant_type_id))
            return ApiResponseHelper::json([], 400, '请选择助教类型');

        /**
         * @var User $user
         */
        $user = User::create(
            array_merge($request->only([
                'name', 'telephone', 'store_id', 'code', 'role_id', 'nickname', 'status', 'avatar', 'parent_user_id',
                'assistant_type_id'
            ]), ['password' => Hash::make($password), 'type' => $userRole->type])
        );

        //Notification Rule
        if (!empty($request->notification_rules)) {

            foreach ($request->notification_rules as $type) {

                $typeValue = $type['value'];
                foreach ($type['children'] as $child) {

                    if ($child['selected'])
                        $user->notificationRules()->create([
                            'type' => $typeValue,
                            'notification_name' => $child['value'],
                        ]);
                }
            }
        }

        return new UserResource($user);
    }

    public function edit(User $user, UserRequest $request)
    {
        if ($user->id == 1)
            return ApiResponseHelper::json([], 400, '超级管理员不可编辑');

        $existUser = User::withTelephone($request->telephone)->first();
        if (!empty($existUser) && $existUser->id != $user->id)
            return ApiResponseHelper::json([], 400, '手机号已存在');

        $userRole = UserRole::find($request->role_id);
        if (empty($userRole))
            return ApiResponseHelper::json([], 400, '角色不存在');

        $password = $request->password;
        $data = $request->only([
            'name', 'telephone', 'store_id', 'code', 'role_id', 'nickname', 'status', 'avatar', 'parent_user_id'
        ]);
        $data['type'] = $userRole->type;
        if (!empty($password))
            $data['password'] = Hash::make($password);

        $user->update($data);

        //Notification Rule
        $user->notificationRules()->delete();
        foreach ($request->notification_rules as $type) {

            $typeValue = $type['value'];
            foreach ($type['children'] as $child) {

                if ($child['selected'])
                    $user->notificationRules()->create([
                        'type' => $typeValue,
                        'notification_name' => $child['value'],
                    ]);
            }
        }

        return new UserResource($user);
    }

    public function remove(User $user)
    {
        if ($user->id == 1)
            return ApiResponseHelper::json([], 400, '超级管理员不可编辑');

        $user->delete();
        return ApiResponseHelper::json([], 200, '删除成功');
    }

    public function getList(Request $request)
    {
        $storeId = $request->input('store_id');
        $type = $request->input('type');

        $query = User::latest();
        if (!empty($storeId))
            $query = $query->where('store_id', $storeId);

        if (!empty($type))
            $query = $query->where('type', $type);

        return UserResource::collection($query->paginate());
    }

    public function getInfo(User $user)
    {
        $user->store;
        return new UserResource($user);
    }
}
